﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Lucene.Net.Documents;
using Winista.Text.HtmlParser;
using OgilvyOne.UKNOW.Entity;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Visitors;
using Winista.Text.HtmlParser.Tags;

namespace OgilvyOne.UKNOW.Search
{
    public class HtmlFileParse
    {

        static List<string>  list = new List<string>();
        /// 获取网页标题
        /// </summary>
        /// <param name="html">html内容</param>
        /// <returns></returns>
        public static  string GetTitle(string html)
        {
            Match m = Regex.Match(html, "<title>(.*)</title>", RegexOptions.IgnoreCase);
            if (m.Groups.Count == 2)
                return m.Groups[1].Value;
            return "文档标题未知";
        }

        /// <summary>
        /// 过滤html TAG标签
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="node"></param>
        public static void FilterTag(SearchField searchField,INode node, string key)
        {
            List<SearchField> searchFieldList = new List<SearchField>();
            if (node.Children != null)
                for (int i = node.Children.Count - 1; i >= 0; i--)
                {
                    if (node.Children[i] is ITag)
                    {
                        ITag t = node.Children[i] as ITag;
                        switch (t.TagName.ToUpper())
                        {
                            case "SCRIPT":
                            case "STYLE":
                                node.Children.Remove(i);

                                break;
                            default:
                                if ((t.TagName.ToUpper() == "P") && (t.ToPlainTextString().Contains(key)))
                                {
                                    
                                    searchField.ContentLst.Add(t.ToPlainTextString());
                                }
                                FilterTag(searchField, node.Children[i],key);
                                break;
                        }

                    }
                }
           

        }
       

        public static void FilterParagraphTag(INode node,string key,ref string resultStr)
        {
            if (node.Children != null)
                for (int i = node.Children.Count - 1; i >= 0; i--)
                {
                    if (node.Children[i] is ITag)
                    {
                        ITag t = node.Children[i] as ITag;
                        switch (t.TagName.ToUpper())
                        {
                            case "SCRIPT":
                            case "STYLE":
                            case "SPAN":
                            case "A":
                                node.Children.Remove(i);
                                break;
                            default:
                                if ((t.TagName.ToUpper() == "P"))
                                {
                                    string str = @"(?<result><p>\s?[^<a].*(?:[\u4e00-\u9fa5].*|[^.])*)";
                                    Regex r = new Regex(str, RegexOptions.IgnoreCase|RegexOptions.Multiline);
                                    Match m = r.Match(t.ToHtml());

                                    resultStr += ((Capture)(m.Groups["result"])).Value;
                                   //resultStr += t.ToHtml();

                                }
                                FilterParagraphTag(node.Children[i], key, ref resultStr);
                                break;
                        }

                    }
                  
                }

        }

        /// <summary>
        /// HTML页面转换成文本
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static  string HtmltoText(string html,string key)
        {
            string strParser = "";
            Lexer lexer1 = new Lexer(html);
            Parser parser1 = new Parser(lexer1);
            NodeFilter body = new TagNameFilter("BODY");
            NodeList nodelistoftitle = parser1.Parse(body);
            for (int i = 0; i < nodelistoftitle.Count; i++)
                FilterParagraphTag(nodelistoftitle[i], key,ref strParser);
           
            return strParser;

        }

        
        /// <summary>
        /// 删除脚本及样式内容
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DelJsStyle(string str)
        {
            string str1 = new Regex(@"(?m)<script[^>]*>(/w|/W)*?</script[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(str, "");
            return new Regex(@"(?m)<style[^>]*>(/w|/W)*?</style[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(str1, "");

        }

        /// <summary>
        /// 删除空格字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Delspace(string str)
        {
            string str1 = new Regex(@"/s+", RegexOptions.Multiline).Replace(str, " ");
            return str1;
        }

    }
}
